import {createStore} from 'vuex'

/**
 * 获取token
 */
function getToken() {
    if (localStorage.getItem("Authorization")) {
        try {
            // @ts-ignore
            let o = JSON.parse(localStorage.getItem("Authorization"));
            if (Date.now() > o.time + o.expire) {
                localStorage.clear()
                o.data = null;
            }else{
                o.time=Date.now();
                localStorage.setItem("Authorization",JSON.stringify(o));
            }
            return o.data;
        } catch (e) {
            localStorage.clear();
        }
    }
    return null;
}

// @ts-ignore
export default createStore({
    state: {
        /**
         * 认证token
         */

        Authorization: getToken(),
        /**
         * 刷新token
         */
        refreshToken: localStorage.getItem("refreshToken") ? localStorage.getItem("refreshToken") : '',
        /**
         * 当前用户
         */
        CurrentUser: localStorage.getItem("CurrentUser"),
        /**
         * 用户权限
         */
        permission: localStorage.getItem("permission"),
        /**
         * 是否刷新页面
         */
        flushPage: true

    },
    //同步修改 state 的 方法第一个参数是state, 第二个参数要修改的值
    mutations: {
        setToken(state, Authorization) {
            state.Authorization = Authorization;
            let tokenObj = {
                data: Authorization,
                time: Date.now(),
                expire: 12 * 60 * 60 * 1000
            }
            localStorage.setItem("Authorization", JSON.stringify(tokenObj));
        },
        refreshToken(state, refreshToken) {
            state.refreshToken = refreshToken;
            localStorage.setItem("refreshToken", refreshToken);
        },
        setCurrentUser(state, user) {
            state.CurrentUser = user;
            localStorage.setItem("CurrentUser", JSON.stringify(user));
        },
        setPermission(state, permission) {
            state.permission = permission;
            localStorage.setItem("permission", JSON.stringify(permission));
        },
        logout(state, other) {
            state.permission = null;
            state.CurrentUser = null;
            state.Authorization = null;
            state.refreshToken = null;
            state.flushPage = true;
            localStorage.clear();
        },
        flushPage(state, active) {
            state.flushPage = active;
        }
    },
    //异步提交 mutations方法 ,第一个参数是store, 第二个参数是 修改的值
    actions: {
        setToken(store, Authorization) {
            // @ts-ignore
            store.commit('setToken', Authorization);
        },
        refreshToken(store, refreshToken) {
            // @ts-ignore
            store.commit('refreshToken', refreshToken);
        },
        setCurrentUser(store, currentUser) {
            store.commit('setCurrentUser', currentUser);
        },
        setPermission(store, permission) {
            store.commit('setPermission', permission);
        },
        logout(store, permission) {
            store.commit('logout', permission);
        },
        flushPage(store, active) {
            store.commit('flushPage', active);
        }
    },
    getters: {
        token(state) {
            return getToken();
        },
        refreshToken(state) {
            if (localStorage.getItem("refreshToken") != null && localStorage.getItem("refreshToken") != "") {
                // @ts-ignore
                return localStorage.getItem("refreshToken");
            } else {
                return null;
            }
        },
        currentUser(state) {
            if (localStorage.getItem("CurrentUser") != null && localStorage.getItem("CurrentUser") != "") {
                // @ts-ignore
                return localStorage.getItem("CurrentUser");
            } else {
                return null;
            }
        },
        permission(state) {
            if (localStorage.getItem("permission") != null && localStorage.getItem("permission") != "") {
                // @ts-ignore
                return localStorage.getItem("permission");
            } else {
                return null;
            }
        }
    },
    modules: {}
})
